import { getMenuListReq } from '@/api/Admin/Menu';
import { RootState } from '@/store/types';
import { ActionContext, ActionTree } from 'vuex';
import { ActionTypes } from './action-types';
import { MutationTypes } from './mutation-types';
import { CommonState } from './types';

export type Actions<S = CommonState, R = RootState> = {
  [ActionTypes.GET_MENU]({ commit }: ActionContext<S, R>): Promise<void>
  [ActionTypes.CLEAR_MENU]({ commit }: ActionContext<S, R>): void
}

export const actions: ActionTree<CommonState, RootState> & Actions = {
  /**
   * 获取用户菜单与路由
   */
  async [ActionTypes.GET_MENU]({ commit }) {
    try {
      const body = await getMenuListReq()
      commit(MutationTypes.SET_MENU, body.data)
    } catch (error) {
      console.log(error);
    }
  },
  [ActionTypes.CLEAR_MENU]({ commit }) {
    commit(MutationTypes.REMOVE_MENU)
  },
}